<html xmlns:th="http://www.thymeleaf.org">
<head th:replace="admin/public/form::head(_)"/>
<body th:replace="admin/public/form::body(~{::div})">
<div class="layui-card">
    <div class="layui-card-header ">
        <span class="layui-breadcrumb">
          <a href="/admin" target="_blank">首页</a>
          <a href="javascript:;">资讯管理</a>
          <a><cite>资讯列表</cite></a>
        </span>
        <a class="layui-btn layui-btn-sm" style="line-height:1.6em;margin-top:3px;float:right"
           href="javascript:location.replace(location.href);" title="刷新">
            <i class="layui-icon layui-icon-refresh-3" style="line-height:30px"></i></a>
    </div>
    <div class="layui-card-body " style="line-height: 38px">
        <form class="layui-form" id="search_form">
            <div class="layui-form-item">
                <div class="layui-input-inline">
                    <select name="categoryId" id="pid-select" lay-verify="required" lay-filter="pid-select"
                            lay-search>
                        <option value="0" data-level="1">请选择分类</option>
                        <option th:each="category:${categories}" th:value="${category.id}" th:text="${category.title}">
                            顶级资讯分类
                        </option>
                    </select>
                </div>
                <div class="layui-input-inline" style="width: 400px">
                    <input type="text" name="title" placeholder="请输入资讯标题" class="layui-input">
                </div>
                <div class="layui-input-inline">
                    <select name="top">
                        <option value="">置顶</option>
                        <option value=true>是</option>
                        <option value=false>否</option>
                    </select>
                </div>
                <div class="layui-input-inline">
                    <select name="hot">
                        <option value="">热门</option>
                        <option value=true>是</option>
                        <option value=false>否</option>
                    </select>
                </div>
                <div class="layui-input-inline">
                    <select name="recommend">
                        <option value="">推荐</option>
                        <option value=true>是</option>
                        <option value=false>否</option>
                    </select>
                </div>

                <div class="layui-input-inline" style="width: 400px">
                    <button class="layui-btn-sm layui-btn" lay-submit="" lay-filter="search">搜索</button>
                    <button type="reset" class="layui-btn-sm layui-btn layui-btn-primary reset">重置</button>
                    <button type="button" class="layui-btn layui-btn-danger layui-btn-sm delAll">批量删除</button>
                    <button type="button" class="layui-btn layui-btn-sm"
                            onclick="parent.xadmin.add_tab('添加资讯','/admin/article/add')">添加
                    </button>
                </div>
            </div>
        </form>

    </div>
    <div class="layui-card-body ">
        <table class="layui-hide" id="test" lay-filter="demo"></table>
        <script type="text/html" id="barDemo">
            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i
                    class="layui-icon layui-icon-edit"></i>编辑</a>
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i
                    class="layui-icon layui-icon-delete"></i>删除</a>
        </script>
    </div>
</div>
</body>
<script>
    layui.use(['table', 'jquery', 'element', 'form'], function () {
        var table = layui.table
        $ = layui.jquery,
            element = layui.element,
            form = layui.form,
            where = {};
        element.render();
        render_table();
        //监听搜索提交
        form.on('submit(search)', function (data) {
            where = data.field;
            render_table(where);
            return false;
        });
        //重置
        $('.reset').on('click', function () {
            render_table();
            $('#search_form')[0].reset();
            layui.form.render();
            where = {};
            return false;
        });

        function render_table(where) {
            var render_obj = {
                elem: '#test'
                , url: '/admin/article/tree'
                , cellMinWidth: 70
                , cols: [
                    [
                        {type: 'checkbox'},
                        {field: 'id', title: 'ID', width: 80, align: 'center', sort: true},
                        {
                            field: 'cover', title: '封面图', align: 'center', height: 150, width: 200
                            , templet: function (d) {
                                return "<img src=\"" + d.cover + "\"></img>";
                            },
                        },
                        {
                            field: 'title', title: '资讯标题', templet: function (d) {
                                return `<a href="/article/${d.id}" target="_blank">${d.title}</a>`;
                            }
                        },
                        {field: 'name', width: 150, title: '所属分类'},
                        {
                            title: '置顶|热门|推荐', align: 'center', width: 300
                            , templet: function (d) {
                                let top = d.top == 1 ? 'checked' : '';
                                let res = '<input type="checkbox" name="top" lay-filter="filter" value="' + d.id + '" lay-skin="switch" lay-text="置顶|置顶"' + top + '>';

                                let hot = d.hot == 1 ? 'checked' : '';
                                res = res + ' <input type="checkbox" name="hot" lay-filter="filter" value="' + d.id + '" lay-skin="switch" lay-text="热门|热门"' + hot + '>';

                                let recommend = d.recommend == 1 ? 'checked' : '';
                                res = res + ' <input type="checkbox" name="recommend" lay-filter="filter" value="' + d.id + '" lay-skin="switch" lay-text="推荐|推荐"' + recommend + '>';
                                return res;
                            }
                        },
                        {title: '操作', width: 200, align: 'center', toolbar: '#barDemo'}
                    ]
                ]
                , page: true
            };
            if (where) {
                render_obj.where = where;//追加搜索条件
            }
            table.render(render_obj);
        }

        //监听工具条
        table.on('tool(demo)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'detail':
                    layer.msg('ID：' + data.id + ' 的查看操作');
                    break;
                case 'del':
                    layer.confirm('确定要删除该文章吗？', function (index) {
                        $.ajax({
                            url: '/admin/article/delete',
                            method: 'post',
                            data: {id: data.id},
                            dataType: 'JSON',
                            success: function (res) {
                                if (res.code == 0) {
                                    obj.del();
                                    layer.msg('删除成功', {icon: 1});
                                } else {
                                    layer.msg(res.msg, {icon: 5});
                                }
                            },
                            error: function (data) {
                                layer.msg('服务器繁忙', {icon: 5});
                            }
                        });
                        layer.close(index);
                    });
                    break;
                case 'edit':
                    xadmin.open('编辑', '/admin/article/edit?id=' + data.id);
                    // layer.alert('编辑行：<br>'+ JSON.stringify(data))
                    break;
                default:
                    break;
            }
        });
        //监听置顶|热门|推荐
        form.on('switch(filter)', function (obj) {
            let id = this.value;
            let data = {id: id};
            data[this.name] = obj.elem.checked ? 1 : 0;
            let __this = this;
            $.ajax({
                url: '/admin/article/ajax',
                method: 'post',
                data: data,
                dataType: 'JSON',
                success: function (res) {
                    if (res.code == 0) {
                        layer.msg('修改成功', {icon: 1});
                    } else {
                        __this.checked = true;
                        form.render();
                        layer.msg(res.msg, {icon: 5});
                    }
                },
                error: function (data) {
                    layer.msg('服务器繁忙', {icon: 5});
                }
            });
        });
        $('.delAll').on('click', function () {
            var checkStatus = table.checkStatus('test');
            var data = checkStatus.data;
            var ids = [];
            if (data.length > 0) {
                for (var i = 0; i < data.length; i++) {
                    ids.push(data[i].id);
                }
                layer.confirm('确定要删除' + data.length + '个文章吗？', function (index) {
                    $.ajax({
                        url: '/admin/article/deleteAll',
                        method: 'post',
                        data: {ids: ids},
                        dataType: 'JSON',
                        success: function (res) {
                            if (res.code == 0) {
                                table.reload('test');
                                layer.msg('删除成功', {icon: 1});
                            } else {
                                layer.msg(res.msg, {icon: 5});
                            }
                        },
                        error: function (data) {
                            layer.msg('服务器繁忙', {icon: 5});
                        }
                    });
                    layer.close(index);
                });
            }
        });

    });
</script>
</html>